
import UIKit


extension UITextView {
    func scrollToBottom(animated: Bool = false) {
        let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.height + contentInset.bottom)
        if bottomOffset.y > 0 {
            setContentOffset(bottomOffset, animated: animated)
        }
    }

}


extension UIScrollView {
    func isAtBottom(tolerance: CGFloat = 2) -> Bool {
        let contentHeight = contentSize.height
        let offsetY = ceil(contentOffset.y)
        let visibleHeight = bounds.height
        return offsetY + visibleHeight >= contentHeight - tolerance
    }
}
